package com.gangwangchina.android.readsms;

import com.gangwangchina.android.readsms.config.Global;
import com.shoushuo.android.tts.ITts;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.widget.Toast;

public class SMSreceiver extends BroadcastReceiver {

	private static final String mACTION = "android.provider.Telephony.SMS_RECEIVED";

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		if (intent.getAction().equals(mACTION)) {
			
			Global.Instance().getContext().sendBroadcast(new Intent("android.intent.action.SCREEN_ON"));
			
			StringBuilder sb = new StringBuilder();
			Bundle bundle = intent.getExtras();
			if (bundle != null) {
				/*
				 * pdus为 android内置短信参数 identifier 通过bundle.get("")返回一包含pdus的对象
				 */
				Object[] myOBJpdus = (Object[]) bundle.get("pdus");
				SmsMessage[] messages = new SmsMessage[myOBJpdus.length];
				for (int i = 0; i < myOBJpdus.length; i++) {
					messages[i] = SmsMessage
							.createFromPdu((byte[]) myOBJpdus[i]);
				}

				
				/* 将送来的短信合并自定义信息于StringBuilder当中 */
				for (SmsMessage currentMessage : messages) {
					sb.append("接收到来自:\n");
					/* 来讯者的电话号码 */
					sb.append(currentMessage.getDisplayOriginatingAddress());
					sb.append("\n------传来的短信------\n");
					/* 取得传来信息的BODY */
					sb.append(currentMessage.getDisplayMessageBody());
					
					Global.Instance().ReadSmsContext(currentMessage.getDisplayOriginatingAddress(), currentMessage.getDisplayMessageBody());
					
				}
			}			
		}
	}
}
